/*

  Munipack - command line parser

  Copyright © 2010-2012 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.


*/

#include "munipack.h"
#include <wx/wx.h>
#include <wx/app.h>
#include <wx/cmdline.h>


MuniCmdLineParser::MuniCmdLineParser(int action, int argc, char *argv[]):
  wxCmdLineParser(argc,argv)  
{
  switch(action) {
  case ID_MDARK:
    AddParam("mdark",wxCMD_LINE_VAL_STRING);
    AddSwitch("r",wxEmptyString,"use arithmetical mean (default: robust mean estimator)");
    AddOption("o",wxEmptyString,"output file");
    AddOption("b",wxEmptyString,"bitpix (-32,32,16,8)");
    AddParam("file(s)",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_MFLAT:
    AddParam("mflat",wxCMD_LINE_VAL_STRING);
    AddOption("n",wxEmptyString,"output mean level");
    AddOption("step",wxEmptyString,"grid step");
    AddOption("o",wxEmptyString,"output file");
    AddOption("b",wxEmptyString,"bitpix (-32,32,16,8)");
    AddParam("file(s)",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_DARK:
    AddParam("dark",wxCMD_LINE_VAL_STRING);
    AddOption("d",wxEmptyString,"dark frame");
    AddSwitch("x",wxEmptyString,"output file flag: ! (overwrite), . (same name, different dir)");
    AddOption("b",wxEmptyString,"bitpix (-32,32,16,8)");
    AddParam("file(s)[,result(s)]",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_FLAT:
    AddParam("flat",wxCMD_LINE_VAL_STRING);
    AddOption("f",wxEmptyString,"flat field");
    AddSwitch("x",wxEmptyString,"output file flag: ! (overwrite), . (same name, different dir)");
    AddOption("b",wxEmptyString,"bitpix (-32,32,16,8)");
    AddParam("file(s)[,result(s)]",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_COLORING:
    AddParam("coloring",wxCMD_LINE_VAL_STRING);
    AddOption("c",wxEmptyString,"colorspace");
    AddOption("ct",wxEmptyString,"color table");
    AddOption("o",wxEmptyString,"output file");
    AddOption("b",wxEmptyString,"bitpix (-32,32,16,8)");
    AddParam("files[,exts]",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_CTRAFO:
    AddParam("ctrafo",wxCMD_LINE_VAL_STRING);
    AddSwitch(wxEmptyString,"list","known colospaces");
    AddOption("c",wxEmptyString,"colorspace");
    AddOption("ct",wxEmptyString,"color table");
    AddOption("o",wxEmptyString,"output file");
    AddOption("b",wxEmptyString,"bitpix (-32,32,16,8)");
    AddParam("file blacks[,weights]",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_CONE:
    AddParam("cone",wxCMD_LINE_VAL_STRING);
    AddSwitch(wxEmptyString,"list","available catalogues");
    AddOption(wxEmptyString,"url","use URL");
    AddOption(wxEmptyString,"cat","use catalogue (--list for list of catalogues)");
    AddOption(wxEmptyString,"par","add parameters");
    AddOption("o",wxEmptyString,"output file");
    AddOption("t",wxEmptyString,"type of output file: fits,csv,txt,xml");
    AddOption("s",wxEmptyString,"sort by the column");
    AddOption("r",wxEmptyString,"search radius in degrees (default 0.1)");
    AddParam("ra,dec ",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    break;

  case ID_VOTABLE:
    AddParam("votable",wxCMD_LINE_VAL_STRING);
    AddOption("o",wxEmptyString,"output file, (-) for stdout");
    AddOption("t",wxEmptyString,"type of output file: fits,csv,txt,svg,xml");
    AddOption("s",wxEmptyString,"sort by the column");
    AddOption("pt",wxEmptyString,"select projection: gnomon");
    AddOption("pa",wxEmptyString,"Right Ascension of projection center");
    AddOption("pd",wxEmptyString,"Declination of projection center");
    AddOption("ps",wxEmptyString,"scale of projection");
    AddOption("ml",wxEmptyString,"magnitude limit");
    AddOption("mk",wxEmptyString,"magnitude field key");
    AddParam("file",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_MULTIPLE);
    //	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_ASTROMETRY:
    AddParam("astrometry",wxCMD_LINE_VAL_STRING);
    AddOption("t",wxEmptyString,"reference table (image)");
    AddOption("c",wxEmptyString,"reference catalogue");
    AddOption(wxEmptyString,"key-alpha","Right Ascension label in catalogue");
    AddOption(wxEmptyString,"key-delta","Declination label in catalogue");
    //    AddOption("o",wxEmptyString,"output FITS");
    AddSwitch("W","wcs-write","write WCS to header");
    AddOption(wxEmptyString,"proj","projection: identity, gnomonic(default)");
    AddOption(wxEmptyString,"xcen","center of projection on chip [pix] (default: width/2)",wxCMD_LINE_VAL_DOUBLE);
    AddOption(wxEmptyString,"ycen","center of projection on chip [pix] (default: height/2)",wxCMD_LINE_VAL_DOUBLE);
    AddOption(wxEmptyString,"acen","center of projection in Right Ascension [deg]",wxCMD_LINE_VAL_DOUBLE);
    AddOption(wxEmptyString,"dcen","center of projection in Declination [deg]",wxCMD_LINE_VAL_DOUBLE);
    AddOption(wxEmptyString,"scale","scale [deg/pix]",wxCMD_LINE_VAL_DOUBLE);
    AddOption(wxEmptyString,"angle","position angle [deg]",wxCMD_LINE_VAL_DOUBLE);
    AddOption(wxEmptyString,"method","fitting method: squares or robust(default)");
    AddOption(wxEmptyString,"units","output units: deg, arcmin, arsec, auto(default)");
    AddOption(wxEmptyString,"tol","uncertainty of centroids of objects [pix]",wxCMD_LINE_VAL_DOUBLE);
    AddSwitch(wxEmptyString,"disable-match","disable inital matching");
    AddOption(wxEmptyString,"nmatch","count of objects in match sequence",wxCMD_LINE_VAL_NUMBER);
    AddOption(wxEmptyString,"maxmatch","count of objects for matching",wxCMD_LINE_VAL_NUMBER);
    AddOption(wxEmptyString,"validity","acceptable validity level",wxCMD_LINE_VAL_DOUBLE);
    AddSwitch(wxEmptyString,"disable-backup","disable backups");
    AddParam("file(s)",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_PHOTOMETRY:
    AddParam("photometry",wxCMD_LINE_VAL_STRING);
    AddOption("f","fwhm","FWHM in pixels");
    AddOption("t","threshold","threshold in sigmas above background");
    AddOption(wxEmptyString,"phpadu","photons per adu");
    AddOption(wxEmptyString,"readns","read noise in ADU");
    AddOption(wxEmptyString,"rndlo","lower for round");
    AddOption(wxEmptyString,"rndhi","higher for round");
    AddOption(wxEmptyString,"shrplo","lower for sharp");
    AddOption(wxEmptyString,"shrphi","higher for sharp");
    AddOption(wxEmptyString,"lothresh","lower for threshold in sigmas");
    AddOption(wxEmptyString,"saturation","saturation in ADU");

    AddParam("file(s)[,output(s)]",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_CALIBRATE:
    AddParam("calibrate",wxCMD_LINE_VAL_STRING);
    AddOption("r",wxEmptyString,"search radius in degrees");
    AddParam("ra,dec file(s)",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_LISTING:
    AddParam("listing",wxCMD_LINE_VAL_STRING);
    AddOption("c","col","column to list (default: apflux1)");
    AddSwitch(wxEmptyString,"mag","output in instrumental magnitudes");
    AddSwitch(wxEmptyString,"full","full listing");
    AddSwitch(wxEmptyString,"pos","position listing");
    AddOption(wxEmptyString,"geo","long,lat geographical coordinates (deg., +east,+north)");
    AddOption(wxEmptyString,"equ","alpha,delta equatorial coordinates (deg.)");
    AddOption(wxEmptyString,"ele","min0,per light curve elements");
    AddOption(wxEmptyString,"tol","search radius");
    AddOption("k",wxEmptyString,"key[,key,..] select header keyword(s)");
    AddParam("x,y [x,y] [idx ...] file(s)",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  case ID_FITS:
    AddParam("fits",wxCMD_LINE_VAL_STRING);
    AddSwitch("k","header","list header");
    AddSwitch("t","table","list table");
    AddSwitch("i","image","list image");
    AddOption("K","keys","key[,key,..] print selected header keyword(s)");
    AddSwitch(wxEmptyString,"short","print header keywords in short form");
    AddParam("file",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    break;

  case ID_KOMBINE:
    AddParam("kombine",wxCMD_LINE_VAL_STRING);
    AddOption("o",wxEmptyString,"output file");
    AddOption("s",wxEmptyString,"scale by specified factor");
    AddOption("i",wxEmptyString,
	      "interpolation method: 0-no, 1-bilinear, 2-bicubic, 3-bspline");
    AddSwitch("f",wxEmptyString,"normalize by flux");
    AddParam("file(s)",wxCMD_LINE_VAL_STRING,
	     wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);
    break;

  default:
    SetLogo(INFOTEXT " " XVERSION ", " COPYLEFT "\n" INFOTEXTFULL);
    AddUsageText("\nGeneral Actions:\n"
		 " corr\tcorrections\n"
		 " calibrate\tcalibrate\n"
		 "\nElemental Actions:\n"
		 " dark\t\tdark frame correction\n"
		 " flat\t\tflat-field correction\n"
		 " mdark\t\taverage dark frame\n"
		 " mflat\t\taverage flat-field\n"
		 " coloring\tcolor composition of frames\n"
		 " kombine\tcomposition of frames\n"
		 " ctrafo\t\tcolor transformations\n"
		 " cone\t\tsearch catalogs (VO-capable cone-search)\n"
		 " astrometry\tastrometry\n"
		 " photometry\tphotometry\n"
		 " listing\tlisting\n"
		 "\nLow Level Actions:\n"
		 " votable\tVO table\n"
		 " fits\t\tFITS file\n"
		 "\n"
		 "Description for actions:\n"
		 " action [-h.--help]\n"
		 " action [options] [file(s)]\n"
		 );

    AddSwitch(wxEmptyString,"version","print version and license");
    AddParam("mdark",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("mflat",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("dark",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("flat",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("kombine",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("coloring",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("ctrafo",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("cone",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("astrometry",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("photometry",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("votable",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("fits",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("calibrate",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("listing",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    AddParam("file(s)",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
    break;
  }  
}

wxArrayString MuniCmdLineParser::GetParams() const
{
  wxArrayString params;

  for(size_t i = 1; i < GetParamCount(); i++)
    params.Add(GetParam(i));

  return params;
}
